<template>
  <div class="ma-content-block lg:flex justify-between">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <template #tableSearch>
        <a-col :span="6">
          <a-form-item field="title" label="标题">
            <a-input v-model="searchForm.title" placeholder="请输入" :allow-clear="true"
              @keydown.enter="refresh"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item field="cate_id" label="分类">
            <a-select :fieldNames="{ value: 'id', label: 'name' }" v-model="searchForm.cate_id" placeholder="请选择"
              :allow-clear="true" @change="refresh">
              <a-option v-for="(item, index) of parasmData.cate" :value="item.id" :label="item.name" />
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="7">
          <a-form-item field="state" label="状态">
            <sa-select v-model="searchForm.state" :allow-clear="true" @change="refresh"
              :data="parasmData.state"></sa-select>
          </a-form-item>
        </a-col>
      </template>
      <template #state="{ record }">
        <a-tag v-if="record.state == 1" color="green">启用</a-tag>
        <a-tag v-if="record.state == 0" color="red">禁用</a-tag>
      </template>
      <template #operationAfterExtend="{ record }">
        <a-button type="primary" size="mini" shape="round"  @click="handleContent(record)">多语言</a-button>
      </template>
    </sa-table>
    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
    <content ref="contentRef"></content>
  </div>
</template>

<script setup>
import { onMounted, ref, reactive, computed } from 'vue'
import noticeApi from '@/api/message/notice'
import { Message } from '@arco-design/web-vue'
import SaTable from '@cps/sa-table/index.vue'
import SaSelect from '@cps/sa-select/index.vue'
import { throttle } from 'lodash';
import EditForm from './edit.vue'
import content from './content.vue'
import tools from '@/utils/tool'
const visible = ref(false)
const submitLoading = ref(false)
const crudRef = ref()
const editRef = ref()
const contentRef = ref()
const searchForm = ref({
  state: 99,
  cate_id: 0,
  title: ''
})
const deleteForm = ref({ //刪除 确认彈窗
  id: '',
  pay_password: '',
})
const options = reactive({
  api: noticeApi.noticeList,//表格数据接口
  operationColumnWidth: 200,//操作欄
  showSearch: true,
  columnAlign: 'center',
  pageSimple: false,//是否展示操作欄有 以及工具欄
  isTreeTable: false,
  add: {//頂部新增按鈕
    show: true,
    auth: ['/core/menu/save'],
    func: async () => {
      editRef.value?.open('add')
    },
  },
  edit: {//操作欄编辑按鈕
    show: true,
    auth: ['/core/menu/update'],
    func: async (record) => {
      editRef.value?.open('edit',record.id)
      editRef.value?.setFormData(record)
    },
  },
  delete: {//操作欄和頂部刪除按鈕
    show: true,
    auth: ['/core/menu/destroy'],
    func: throttle(async (params) => {
      deleteForm.value.id = params.ids[0]
      const { data } = await noticeApi.delNotice(deleteForm.value)
      Message.success(`刪除成功`)
      refresh()
    }, 2000)
  },
  isExpand: false,//展開所有子樹
})
const columns = reactive([
  { title: 'ID', dataIndex: 'id', width: 100 },
  { title: '语言数量', dataIndex: 'lang_count', width: 180 },
  { title: '类型', dataIndex: 'cate_text', width: 160 },
  { title: '状态', dataIndex: 'state', width: 180 },
  { title: '标题', dataIndex: 'title', width: 180 },
  { title: '排序', dataIndex: 'sort', width: 180 },
  { title: '创建时间', dataIndex: 'create_time', width: 180 },
  { title: '修改时间', dataIndex: 'update_time', width: 180 },
])
const handleContent = (data) => {
  contentRef.value?.open(data)
}
const initPage = async () => {

}
const parasmData = ref({})
const getParasm = async () => {
  const { data } = await noticeApi.noticeListParams()
  parasmData.value = { ...data }
  parasmData.value.state = tools.simpleTransformArray(data.state)
  parasmData.value.state.unshift({ value: 99, title: '全部' })
  parasmData.value.cate.unshift({ id: 0, name: '全部' })
}
const refresh = async () => {
  crudRef.value?.refresh()
}


onMounted(async () => {
  initPage()
  refresh()
  getParasm()
})
</script>

<style scoped>
.icon {
  width: 1em;
}
</style>